function [name,nameLabel,value,nameFlag] = parsenames(code)
% parsenames [Not a public function] Parse names within a name block.
%
% Backend IRIS function.
% No help provided.

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

import preparser.White;

% Parse names with labels and assignments.
% @@@@@ MOSW.
% Extra pair of brackets needed in Octave.
NAME_PATTERN = [ ...
    '(("[ ]*"|''[ ]*'')?)\s*', ... % Label.
    '([a-zA-Z]\w*)\s*', ... % Name.
    '((=[^;,\n]+[;,\n])?)', ... % Value.
    ];

%--------------------------------------------------------------------------

if isempty(code)
    name = cell(1,0);
    nameLabel = cell(1,0);
    value = cell(1,0);
    nameFlag = false(1,0);
    return
end

code = [code,sprintf('\n')];
whCode = code;

% White out the inside of labels (single or double qouted text), keeping
% the quotation marks.
whCode = White.whiteOutLabel(whCode);

% White out first-level round and square brackets. This is to handle
% assignments containing function calls with multiple arguments separated
% with commas (commas are valid separator of parameters).
whCode = White.whiteOutParenth(whCode,1);

tknExt = regexp(whCode, NAME_PATTERN, 'tokenExtents');

if true % ##### MOSW
    % Do nothing.
else
    for i = 1 : length(tknExt) %#ok<UNRCH>
        if size(tknExt{i},1)==2
            tknExt{i} = [ {[1,0]}; tknExt{i} ];
        end
    end
end

nName = length(tknExt);
nameLabel = cell(1,nName);
name = cell(1,nName);
value = cell(1,nName);
nameFlag = false(1,nName);
for i = 1 : nName
    nameLabel{i} = code( tknExt{i}(1,1)+1 : tknExt{i}(1,2)-1 );
    name{i}  = code( tknExt{i}(2,1)   : tknExt{i}(2,2)   );
    value{i} = strtrim(code( tknExt{i}(3,1)+1   : tknExt{i}(3,2)   ));
end

end
